from sqlalchemy import Column, Integer, String, Text, DateTime
from datetime import datetime
from app.utils.db import Base

class Movie(Base):
    """
    影视资源模型
    """
    __tablename__ = 'movies'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)  # 资源名称
    desc = Column(Text)  # 资源描述
    url = Column(String(255))  # 采集 URL
    play_url = Column(String(255))  # 播放器 URL
    is_active = Column(Integer, default=0)  # 是否有效
    created_at = Column(DateTime, default=datetime.now)  # 创建时间
    updated_at = Column(DateTime)  # 更新时间
    
    def __repr__(self):
        return f"<Movie(id={self.id}, name={self.name})>"
    
    def to_dict(self):
        """
        将对象转换为字典，方便JSON序列化和API响应
        """
        return {
            "id": self.id,
            "name": self.name,
            "desc": self.desc,
            "url": self.url,
            "play_url": self.play_url,
            "is_active": self.is_active,
            "created_at": self.created_at.strftime("%Y-%m-%d %H:%M:%S") if self.created_at else None,
            "updated_at": self.updated_at.strftime("%Y-%m-%d %H:%M:%S") if self.updated_at else None
        } 